package com.glebusheg.ui 
{
	import com.glebusheg.data.AppData;
	import com.glebusheg.data.Item;
	import com.glebusheg.data.ItemType;
	import com.glebusheg.debug.Console;
	import com.glebusheg.utils.VisualUtil;
	import com.greensock.TweenLite;
	import flash.display.Bitmap;
	import flash.display.Sprite;
	/**
	 * ...
	 * @author Gleb Volkov
	 */
	public class AppItemControl extends ItemControlBase
	{
		private var _item:Item;
		private var _icon:IconControl;
		
		public function AppItemControl() 
		{
			mouseEnabled = false;
			
			if (this.hasOwnProperty("icon"))
			{
				_icon = new IconControl(this["icon"]);
				_icon.onLoad = onIconLoad;
			}
			
			// setup for all other content elements
		}
		
		public function setupItem(name:String):void
		{
			if (!_item)
			{
				var item:Item;
				if(name) item = AppData.getItem(name);
				if (item)
				{
					_item = item;
					
					if (_icon)
					{
						//_icon.loadIcon(null); // reset
						_icon.loadIcon(_item.info.iconResource);
					}
				}
			}
		}
		
		
		private function onIconLoad(icon:Bitmap):void
		{
			if (_item && _item.type == ItemType.CLOTHES)
			{
				resizeIconContent(icon, 40);
				
				icon.alpha = 0;
				TweenLite.to(icon, 0.5, { alpha:1 } );
			}
		}
		
		private function resizeIconContent(icon:Bitmap, size:int):void
		{
			var maxSide:int = int(Math.max(icon.width, icon.height));
			if (maxSide > size)
				icon.scaleX = icon.scaleY = size / maxSide;
		}
		
		public function destroy():void 
		{
			_item = null;
			
			_icon.destroy();
			_icon = null;
		}
		
		public function get item():Item 
		{
			return _item;
		}
		
	}

}